package com.easyfun.healthmagicbox.bluetooth.ycgi;

import android.util.Log;

import com.easyfun.healthmagicbox.bluetooth.MessageParser;

/**
 * 
 * @author Teddy
 * the message flow :
 * 4F 06 00 00 06 02 F1 00 06 01 01 BA
 * 4F 06 00 00 06 02 F1 00 06 02 01 B9
 * 4F 06 00 00 06 02 F1 00 06 03 01 B8
 * 4F 06 00 00 06 02 F1 00 06 04 01 BF 
 * 4F 06 00 00 06 02 F1 00 06 05 01 BE 
 * 51 06 00 00 0C 02 F1 06 01 00 2C 14 01 21 15 52 00 F0 
 * 4F 06 00 00 03 02 F1 FE 47 
 * 
 * others:
 * Detect response:
 * 5F 06 00 00 06 02 F1 00 05 01 2C 84
 * Control response:
 * 51 06 00 00 04 02 F1 FC 02 5E
 * Version response:
 * 51 06 00 00 07 02 F1 FB 00 01 00 00 59
 * Empty data response:
 * 51 06 00 00 0C 02 F1 06 01 00 00 00 00 00 00 00 00 AF 
 */
public class YiChengGIParser extends MessageParser<YiChengGIMessage>{

	private static final String TAG = YiChengGIParser.class.getSimpleName();

	@Override
	public YiChengGIMessage parse(byte[] buffer1, int len) {
		YiChengGIMessage msg = new YiChengGIMessage();
		System.arraycopy(buffer1, 0, innerBuffer, innerLen, len);
		innerLen = innerLen + len;
		if (innerLen > innerBuffer.length)
		{
			Log.e(TAG, "YICheng overflow");
			return null;
		}
		
		byte[] rawMessage = getIntegratedMessage();
		if (rawMessage == null)
		{
			return null;
		}else
		{
			msg.setRawBytes(rawMessage);
		}
		msg.setMsgType(YiChengGIMsgType.NONE);
		if ((rawMessage[7] == 0x00)&&(rawMessage[8] == 0x06))
		{
			msg.setMsgType(YiChengGIMsgType.WORKFLOW_RESPONSE);
			msg.setWorkFlowStep(rawMessage[9]);
		}else if ((rawMessage[7] == 0x06)&&(rawMessage[8] == 0x01))
		{
			msg.setMsgType(YiChengGIMsgType.DATA_RESPONSE);
			msg.setBloodsugar(rawMessage[9],rawMessage[10]);
			msg.setDatetime(new byte[]{rawMessage[11],rawMessage[12],rawMessage[13],rawMessage[14],rawMessage[15],rawMessage[16]});
		}else if ((rawMessage[7] == 0x00)&&(rawMessage[8] == 0x05))
		{
			msg.setMsgType(YiChengGIMsgType.DETECT_RESPONSE);
		}else if ((0xFF & rawMessage[7]) == 0xFC)
		{
			msg.setMsgType(YiChengGIMsgType.CONTROL_RESPONSE);
		}else if ((0xFF & rawMessage[7]) == 0xFB)
		{
			msg.setMsgType(YiChengGIMsgType.VERSION_RESPONSE);
		}else if ((0xFF & rawMessage[7]) == 0xFE)
		{
			msg.setMsgType(YiChengGIMsgType.CLOSE_RESPONSE);
		}
		
		return msg;
	}
	
	/**
	 *   0=====1=====[2===3]====4=======[5====6]========7======[N+4]=====N+5
	 * header 0x06   device ID  length   append    param type   load   jiaoyan
	 *                                   address
	 *                          N        
	 */
	@Override
	protected byte[] getIntegratedMessage() {
		if (innerBuffer.length < 8)
		{
			return null;
		}
		int begin = 0;
		int end = 0;
		byte[] integrateMsg = null;
		for (int i=0; i< innerLen; i++)
		{
			int firstbyte = 0xFF&innerBuffer[i];
			int secondbyte = 0xFF&innerBuffer[i + 1];
			if (((firstbyte==0x4F) || (firstbyte== 0x51) ||
					(firstbyte== 0x5F) || (firstbyte==0x41)) &&
					(secondbyte == 0x06)
					)
			{
				int loadsize = innerBuffer[i+4];
				if ( (innerBuffer.length - i) > (loadsize + 4) + 1 ) 
					//1 is size itself and > is jiaoyan
				{
					begin = i;
				}
				int tmpend = i + (loadsize + 4) + 1 + 1;
				if (innerLen >= tmpend)
				{
					end = i + (loadsize + 4) + 1 + 1;
				}

				if ((end != 0))
				{
					integrateMsg = rearrangeBuffer(begin,end);
				} 
				return integrateMsg;
			}
			
		}
		return null;
		
	}

}
